package iblink.adapter;

import packutil.DateUtil;
import packutil.exception.UnimplementedOperationException;
import iblink.core.ISecurity;
import iblink.core.Option;
import iblink.core.Stock;
import iblink.exception.IBLinkException;

import com.ib.client.Contract;
import com.ib.client.ContractDetails;

public class IBHelper {

	public static boolean isUSExchange(String exchange) {

		if (exchange.equals("NYSE") || exchange.equals("NASDAQ")
				|| exchange.equals("AMEX") || exchange.equals(""))
			return true;

		return false;
	}

	public static Stock parseContractDetailsToStock(ContractDetails cd) {

		if (!"STK".equals(cd.m_summary.m_secType))
			throw new IBLinkException(
					"Attempting to parse ContractDetails into Stock but sec type is '"
							+ cd.m_summary.m_secType + "', not 'STK'");

		String symbol = cd.m_summary.m_symbol;
		String longName = cd.m_longName;
		String industry = cd.m_industry;
		String category = cd.m_category;
		String subcategory = cd.m_subcategory;
		String primaryExchange = cd.m_summary.m_primaryExch;

		return new Stock(symbol, longName, industry, category, subcategory,
				primaryExchange);
	}

	public static Contract getStockContract(String symbol) {
		Contract contract = new Contract();
		contract.m_secType = "STK";
		contract.m_symbol = symbol;
		contract.m_currency = "USD";
		contract.m_exchange = "SMART";
		return contract;
	}

	public static Contract getStockContract(Stock s) {
		Contract contract = new Contract();
		contract.m_secType = "STK";
		contract.m_currency = "USD";
		contract.m_exchange = "SMART";
		contract.m_symbol = s.getSymbol();
		contract.m_primaryExch = s.getPrimaryExchange();
		return contract;
	}

	public static Contract getContract(ISecurity sec) {

		if (sec instanceof Stock) {
			return getStockContract((Stock) sec);
		} else if (sec instanceof Option) {
			return getOptionContract((Option) sec);
		}

		throw new IBLinkException("Get contract not implemented for class "
				+ sec.getClass().getSimpleName());
	}

	public static Contract getOptionContractForUnderlying(ISecurity underlying) {
		Contract c = getContract(underlying);
		c.m_secType = "OPT";
		return c;
	}

	private static Contract getOptionContract(Option option) {

		Contract contract = new Contract();
		contract.m_exchange = "SMART";
		contract.m_currency = "USD";
		contract.m_secType = "OPT";
		contract.m_right = option.getRight().getIbString();
		contract.m_strike = option.getStrikePrice();
		contract.m_expiry = DateUtil.formatDate(option.getExpirationDate(),
				"yyyyMMdd");

		if (option.getUnderlying() instanceof Stock) {
			contract.m_symbol = ((Stock) option.getUnderlying()).getSymbol();
		} else {
			throw new UnimplementedOperationException(
					"Trying to create contract for option with underlying["
							+ option.getUnderlying()
							+ "] that is not a stock this functionality not implemented.");
		}

		return contract;
	}

}
